Public Class frmNhapThuoc
    Public fCallVattu As Form
    Private m_macha As String
    Private m_loai As Boolean  'loai   True:Them   false:Sua
    Dim ma_thuoccu As String 'luu ma thuoc truoc khi sua ma

#Region "GhiChu"
    'ThemSua: Neu la Them thi macha chinh la ma dua vao
    '         Neu la Sua thi ma can sua la macha dua vao va ma sua la ma nhap vao
    '         Nhu vay se xu ly macha dua vao neu la them thi se bot so 0 dang sau
    '                                        neu la sua thi de nguyen
#End Region

    Public Sub New(ByVal macha As String, ByVal loai As Boolean) 'loai them || sua
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        m_macha = macha
        ma_thuoccu = macha
        m_loai = loai
        'loai de phan biet them: true hay sua: false
        KhoiTao(loai)
    End Sub

    Private Sub frmNhapLoaiThuoc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mtbMa.Focus()
    End Sub

    Public Sub KhoiTao(ByVal loai As Boolean)
        If (loai) Then 'them
            mtbMa.Mask = m_macha.Substring(0, 7) + "#######"
            mtbMa.Text = m_macha.Substring(0, 7)
            txtTen.Text = ""
            mtbMa.ReadOnly = False
        Else 'sua
            Dim objThuoc As CThuoc
            objThuoc = CThuocs.GetThuocByMa(m_macha)
            mtbMa.Mask = "Y#############"
            mtbMa.Text = m_macha
            'mtbMa.ReadOnly = True
            txtTen.Text = objThuoc.TenThuoc
            txtDVT.Text = objThuoc.DonViTinh
            m_macha = objThuoc.MaLoaiThuoc
        End If
    End Sub

    Function KiemTraNhapLoaiThuoc() As Boolean
        If (txtTen.Text.Trim = "") Then
            ShowMessages("Bạn chưa nhập tên loại thuốc")
            Return False
        Else
            If (mtbMa.Text.Length <> 14) Then
                ShowMessages("Bạn nhập chưa đúng mã thuốc")
                Return False
            End If
        End If
        If (m_loai) Then 'them
            Dim str As String
            str = "select count(*) from tblDMTHUOC where mathuoc = '" & mtbMa.Text & "'"
            If (BaseDB.ExecSql_DataValue(str) <> 0) Then
                ShowMessages("Đã tồn tại mã thuốc này")
                Return False
            End If
        Else 'sua
            If (mtbMa.Text <> ma_thuoccu) Then
                Dim str As String
                str = "select count(*) from tblDMTHUOC where mathuoc = '" & mtbMa.Text & "'"
                If (BaseDB.ExecSql_DataValue(str) <> 0) Then
                    ShowMessages("Đã tồn tại mã thuốc này")
                    Return False
                End If
            End If
        End If
        Return True
    End Function

    Private Sub btnGhiThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGhiThem.Click
        If (KiemTraNhapLoaiThuoc() = False) Then
            Return
        End If
        Dim objThuoc As New CThuoc
        objThuoc.MaThuoc = mtbMa.Text
        objThuoc.TenThuoc = txtTen.Text
        objThuoc.MaLoaiThuoc = m_macha
        objThuoc.DonViTinh = txtDVT.Text
        If (m_loai) Then 'them
            CThuocs.InsertThuoc(objThuoc)
        Else 'sua
            CThuocs.UpdateThuoc(objThuoc, ma_thuoccu)
        End If
        'load moi
        Try
            If fCallVattu.Name = "frmVattu" Then
                Dim frm As frmVatTu = CType(fCallVattu, frmVatTu)
                frm.LoaddsThuoc()
                frm.KhoiTaoLvThuoc(m_macha, objThuoc.MaThuoc)
            Else
                Dim frm As frmChonMatHang = CType(fCallVattu, frmChonMatHang)
                frm.LoaddsThuoc()
                frm.KhoiTaoLvThuoc(m_macha, objThuoc.MaThuoc)
            End If
            
        Catch ex As Exception
        End Try
        'them 
        m_loai = True
        KhoiTao(True)
    End Sub

#Region "txtDVT"
    Private Sub txtDVT_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDVT.DoubleClick
        HienDMDVT()
    End Sub

    Public Sub HienDMDVT()
        Dim frm As New frmDMDoiTuong
        frm.eDoiTuong = enumDOITUONG.DVT
        frm.bCallForm = True
        frm.ShowDialog()
        txtDVT.Text = frm.selectedDT.Ten
    End Sub

    Private Sub txtDVT_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDVT.KeyDown
        If (e.KeyCode = Keys.F1) Then
            HienDMDVT()
        End If
    End Sub
#End Region

End Class